package com.dyl.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dyl.entity.Classes;
import com.dyl.entity.Student;
import com.dyl.service.ClassesService;
import com.dyl.service.StudentService;
import com.dyl.mapper.StudentMapper;
import com.dyl.util.PageResult;
import com.dyl.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
* @author wxq12
* @description 针对表【student】的数据库操作Service实现
* @createDate 2025-01-07 09:17:25
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    private ClassesService classesService;

    //    1.分页查询
    @Override
    public PageResult<Student> findByPage(Integer page, Integer pageSize) {
//        1。1开始分页
        Page<Student> page1 = this.page(new Page<>(page, pageSize));
//        2。封装分页结果
        return new PageResult<>(page1.getRecords(), page1.getTotal());
    }

    //3.条件带分页查询
    @Override
    public PageResult<Student> search(Integer page, Integer pageSize, StudentVO vo) {
//        1.进行分页带条件查询
        //1.1 封装查询条件
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getSname()), "sname", vo.getSname());
        wrapper.like(StrUtil.isNotBlank(vo.getAddr()), "addr", vo.getAddr());
        wrapper.eq(vo.getCid() !=null && vo.getCid() !=0 , "cid",vo.getCid());

        // 1.2 开始分页查询
        Page<Student> page1 = this.page(new Page<>(page, pageSize), wrapper);

        // 1.3 处理学生所在班级名称
        page1.getRecords().forEach(st -> {
            Classes classes = classesService.getById(st.getCid());
            st.setCname(classes.getCname());
        });

        // 2. 封装分页结果
        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }
}







